package com.project.controller;

import com.project.entity.Result;
import com.project.entity.User;
import com.project.service.UserService;
import com.project.util.JWTUtil;
import com.project.util.MD5Util;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.net.http.HttpRequest;

import static com.project.util.MD5Util.md5key;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping("/login")
    public Result login(String username, String password, HttpServletRequest request, HttpServletResponse response) {
        //System.out.println(username);
        //System.out.println(password);

        Result result = new Result();


        try{
            //根据用户名产找用户
            User user = userService.findOneByUserName(username);

            String s = MD5Util.md5(password,md5key);
            if(user == null){

                return result.error("用户名不正确");
            }else {

                if(!user.getPassword().equals(s)){

                    return result.error("密码不正确");
                }else {
                    //登陆成功,发送令牌
                    String token = JWTUtil.createToken(username);
                    //将用户存放到session中
                    request.getSession().setAttribute("loginUser", user);
                    result.putData("user",user);

                    //采用响应头返回token
                    response.setHeader("authorize",token);
                    return result.success("登陆成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            result.error("后端报错");
        }

        return result;
    }



    @PostMapping("/register")
    public Result register(User user) {
        //

        Result result = new Result();
        try {
            //判断用户是否已经使用
            User user1 = userService.findOneByUserName(user.getUserName());
            if(user1 != null){
                //用户名使用
                return result.error("用户名已经被占用了，请换一个注册");
            }else {
                String pass = MD5Util.md5(user.getPassword(),md5key);
                user.setPassword(pass);
                userService.save(user);
                result.success("注册成功");
            }


        }catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    @GetMapping("/getInfo/{userName}")
    public Result getInfo(@PathVariable String userName) {
        Result result = new Result();
        try {
            User user = userService.getInfo(userName);
            result.success("成功");
            result.putData("user",user);
        }catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }

    @PostMapping("/update")
    public Result update(User user) {
        Result result = new Result();
        try {
            userService.update(user);
            result.success("成功");
            result.putData("user",user);
        }catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }

    @GetMapping("logout")
    public Result logout(HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        System.out.println("删除");
        try {
            request.getSession().removeAttribute("loginUser");
            response.setHeader("authorize",null);
            result.success("成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
}
